package ahblue.cn.tanzi.ui.person.pay;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.StyleRes;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;

import ahblue.cn.tanzi.R;

public abstract class BDialog extends Dialog {
    private Context context;
    private int mGravity = Gravity.CENTER;
    private int defaultPadwidth = 0;
    private int defaultPadHeight = 0;
    private boolean islockScreen = false;

    //dialog_transparent
    public BDialog(Context context) {
        this(context, R.style.dialog_transparent);
        setOwnerActivity((Activity) context);
        this.context = context;
    }

    public BDialog(Context context, @StyleRes int themeResId) {
        super(context, themeResId);
        this.context = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initListener();
    }

    private void initListener() {
        setOnShowListener(new OnShowListener() {
            @Override
            public void onShow(DialogInterface dialogInterface) {
                WindowManager windowManager = ((Activity) context).getWindowManager();
                Display display = windowManager.getDefaultDisplay();
                WindowManager.LayoutParams lp = getWindow().getAttributes();
                lp.width = (int) (display.getWidth()) - setPadWidth(defaultPadwidth); //设置宽度
//                lp.height = (int) (display.getHeight()) - setPadHeight(defaultPadHeight);
                getWindow().setAttributes(lp);
                getWindow().setGravity(setGravity(mGravity));
//                setCancelable(isCancelable(true));
//                setCanceledOnTouchOutside(isCancelable(true));
            }
        });
    }

    /**
     * dialog按返回键和空白处是否可以消失
     *
     * @param b
     * @return
     */
    public abstract boolean isCancelable(boolean b);

    /**
     * 设置dialog的Gravity属性
     * 最终停留的位置
     * @param gravity
     * @return
     */
    public abstract int setGravity(int gravity);

    /**
     * 设置dialog横向距离边缘的长度
     *
     * @param width
     * @return
     */
    public abstract int setPadWidth(float width);

    /**
     * 设置dialog纵向距离边沿的长度
     *
     * @param height
     * @return
     */
    public abstract int setPadHeight(float height);

    /**
     * 获取rootView
     * @return
     */
    public abstract View getRootView();

    // 根据手机的分辨率将dp的单位转成px(像素)
    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }


    @Override
    public void show() {
        super.show();
    }
}